<script setup lang="ts">
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import Search from "@iconify-icons/ep/search";
import Refresh from "@iconify-icons/ep/refresh";
import router from "@/router";
import { getMeterList, getResidentialList, meterCalc, uploadFile } from "@/api/property";
import { reactive, ref } from "vue";
import { baseUrlApi } from "@/api/utils";
import { useUserStore } from "@/store/modules/user";

const userStore = useUserStore();
defineOptions({
  name: "meterList"
});
const form = reactive({
  name: ""
});
const loading = ref(true);
const dataList = ref([]);
const residentialList = ref([]);
const residential = ref(null);//小区ID
const meterType = ref(null);//计量表类型
const action = ref(baseUrlApi("property/meterItem/uploadFile"));
const header = ref({ "Authorization": userStore.token });

//初始化数据
async function onSearch() {
  //获取小区列表
  getResidentialList().then(res => {
    residentialList.value = res.data.records;
  });
  loading.value = false;
}

const getMeter = () => {
  getMeterList({ residentialId: residential.value, meterType: meterType.value }).then(res => {
    dataList.value = res.data;
  });
};
//账单结算
const calc = (data?: any) => {
  meterCalc(data).then(res => {
    if (res.code === 200) {
      getMeter();
    }
  });
};
onSearch();

</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px]"
    >
      <el-form-item>
        <el-upload
          ref="upFile"
          :action="action"
          class="upload-demo"
          :limit="1"
          :auto-upload="true"
          :headers="header"
        >
          <el-button class="ml-3" type="success">
            选择文件
          </el-button>
        </el-upload>
      </el-form-item>
      <br />
      <el-form-item label="归属小区">
        <el-radio-group
          v-model="residential"
          size="small"
        >
          <el-radio-button
            v-for="list in residentialList"
            :label="list.id"
            :value="list.id"
          >{{ list.name }}
          </el-radio-button
          >
        </el-radio-group>
      </el-form-item>
      <br />
      <el-form-item label="计量表类型">
        <el-radio-group
          v-model="meterType"
          size="small"
          @change="getMeter"
        >
          <el-radio-button label="1">
            水表
          </el-radio-button>
          <el-radio-button label="2">
            电表
          </el-radio-button>
          <el-radio-button label="3">
            天然气表
          </el-radio-button>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <el-table :data="dataList.records" table-layout="fixed">
      <el-table-column prop="id" label="ID" />
      <el-table-column prop="homeId" label="房屋ID" />
      <el-table-column label="表具类型">
        <template #default="scope">
          <el-tag v-if="scope.row.type===1">水表</el-tag>
          <el-tag v-if="scope.row.type===2">燃气表</el-tag>
          <el-tag v-if="scope.row.type===3">电表</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="meterItem[0].data" label="计量数据" />
      <el-table-column prop="meterItem[0].updated" label="计量时间" />
      <el-table-column label="结算状态">
        <template #default="scope">
          <el-tag v-if="scope.row.meterItem[0]!=null&&scope.row.meterItem[0].status===0">未结算</el-tag>
          <el-tag v-if="scope.row.meterItem[0]!=null&&scope.row.meterItem[0].status===1">已结算</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button
            type="danger"
            size="small"
            v-if="scope.row.meterItem[0]!=null&&scope.row.meterItem[0].status===0"
            @click="calc(scope.row.meterItem[0].meterId)"
          >立即结算
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="width: 100%">
      <el-pagination
        style="float: right"
        background
        layout="prev, pager, next"
        :total="dataList.total"
        :page-count="dataList.current"
      />
    </div>
    <div>
      <!--  使用Dialog弹窗进行出租-->
      <el-dialog v-model="rentDialog" title="房屋交房" width="500" center>
    <span>
      用户ID：<el-input v-model="rentData.userId"></el-input>
    </span>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="rentDialog = false">取消</el-button>
            <el-button type="primary" @click="rentData.deliveryStatus = '1';rent();rentDialog = false">
              确定
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<style scoped lang="scss">

</style>
